package com.guigusuqi.commonutils.utils;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.guigusuqi.commonutils.vo.Result;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;

public class AliYunOSSUtils
{
    //七牛云密钥对应的ky-value
    @Value("${aliyun.accessKey}")
    private String accessKeyId;

    @Value("${aliyun.accessSecretKey}")
    private String accessKeySecret;

    @Value("${aliyun.endpoint}")
    private String endpoint;

    @Value("${aliyun.bucketName}")
    private String bucketName;

    @Value("${aliyun.customDomainName}")
    private String customDomainName;

    /*
     * 上传文件
     */
    public Result uploadDocument(MultipartFile multipartFile)
    {
        String name = multipartFile.getOriginalFilename();

        //获取OSS连接
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        StringBuilder url = new StringBuilder();
        //通过ossClient来获取上传文件后返回的url
        try {
            ossClient.putObject(bucketName, name, new ByteArrayInputStream(multipartFile.getBytes()));
            url.append(customDomainName).append(name);
        }catch (Exception e)
        {
            e.printStackTrace();
        }
        return Result.success().data(url.toString());
    }

    /**
     * @param FilenameExtension
     * @Author GuoJin Lv
     * @Description 判断OSS服务文件上传时文件的类型contentType
     * @Return String
     * @Date 2021/8/31 13:09
     */
    public String getContentType(String FilenameExtension)
    {

        if (FilenameExtension.equalsIgnoreCase(".bmp")) {
            return "image/bmp";
        }
        if (FilenameExtension.equalsIgnoreCase(".gif")) {
            return "image/gif";
        }
        if (FilenameExtension.equalsIgnoreCase(".jpeg") ||
                FilenameExtension.equalsIgnoreCase(".jpg") ||
                FilenameExtension.equalsIgnoreCase(".png")) {
            return "image/jpeg";
        }
        if (FilenameExtension.equalsIgnoreCase(".html")) {
            return "text/html";
        }
        if (FilenameExtension.equalsIgnoreCase(".txt")) {
            return "text/plain";
        }
        if (FilenameExtension.equalsIgnoreCase(".vsd")) {
            return "application/vnd.visio";
        }
        if (FilenameExtension.equalsIgnoreCase(".pptx") ||
                FilenameExtension.equalsIgnoreCase(".ppt")) {
            return "application/vnd.ms-powerpoint";
        }
        if (FilenameExtension.equalsIgnoreCase(".docx") ||
                FilenameExtension.equalsIgnoreCase(".doc")) {
            return "application/msword";
        }
        if (FilenameExtension.equalsIgnoreCase(".xml")) {
            return "text/xml";
        }
        return "image/jpeg";
    }

    public Boolean fileIsExist(String filename)
    {
        //  创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        //  判断文件是否存在。如果返回值为true，则文件存在，否则存储空间或者文件不存在。
        //  设置是否进行重定向或者镜像回源。默认值为true，表示忽略302重定向和镜像回源；如果设置isINoss为false，则进行302重定向或者镜像回源。
        //  boolean isINoss = true;
        //  填写Bucket名称和Object完整路径。Object完整路径中不能包含Bucket名称。
        boolean found = ossClient.doesObjectExist(bucketName, filename);
        //  boolean found = ossClient.doesObjectExist("examplebucket", "exampleobject.txt", isINoss);
        //  System.out.println(found);

        //  关闭OSSClient。
        ossClient.shutdown();

        return found;
    }

}
